//
//  MineViewController.m
//  DaMaoMall
//
//  Created by HXHL-wally on 16/7/8.
//  Copyright © 2016年 daMao-wally. All rights reserved.
//

#import "MineViewController.h"
#import "LoginViewController.h"
#import "CollectManageViewController.h"
@interface MineViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView * Minetableview;
@property (nonatomic, strong) NSArray * dataArray;
@property (nonatomic, strong) UIButton * btn;
@property (nonatomic, strong) UIButton * avatarBTN;
@property (nonatomic, strong) UILabel * INC;
@property (nonatomic, strong) UILabel * vipName;
@property (nonatomic, strong) UIImageView * headerImg;
@property (nonatomic, strong) UILabel * identification;


@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.leftBtn.hidden = YES;
    self.titleLabel.text = @"个人中心";
    
    [self setUpTableView];
    
    self.dataArray = @[@[@"收藏管理"],@[@"会员服务",@"招采信息",@"用户认证"],@[@"申请看货",@"供应商管理",@"加盟管理",@"退出账号"]];
//    [self.navigationController.tabBarItem setBadgeValue:@""];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - 创建tableview
- (void)setUpTableView{
    UITableView * tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, GetScreenWidth, GetScreenHeight-113) style:UITableViewStyleGrouped];
    self.Minetableview = tableView;
    self.Minetableview.delegate = self;
    self.Minetableview.dataSource = self;
//    self.Minetableview.separatorStyle = UITableViewCellAccessoryNone;
    [self.contentView addSubview:self.Minetableview];
    
}
#pragma mark - 解决cell分割线不到左边界问题
-(void)viewDidLayoutSubviews {
    
    if ([self.Minetableview respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.Minetableview setSeparatorInset:UIEdgeInsetsZero];
        
    }
    if ([self.Minetableview respondsToSelector:@selector(setLayoutMargins:)])  {
        [self.Minetableview setLayoutMargins:UIEdgeInsetsZero];
    }
    
}
/**
 * 这个方法和上面一个方法解决cell分割线不到左边界的问题
 */
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0){
        return 1;
    }
    if (section == 1) {
        return 3;
    }
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50.0f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section == 0){
        CGFloat adViewH = GetScreenHeight * 0.15;
        return adViewH;
    }
    return 10.0f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * headerView =[[UIView alloc]init];
    
    if(section !=0){
        return nil;
    }else{
        
        UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, GetScreenWidth, GetScreenHeight*0.15)];
        self.btn = btn;
        [self.btn addTarget:self action:@selector(clickHeadView) forControlEvents:UIControlEventTouchUpInside];
        Account * account =[DMAccountTool account];
        if (account) { // 登录成功
            self.INC.text = account.nickname;
            [self.headerImg sd_setImageWithURL:[NSURL URLWithString:account.avatar] placeholderImage:[UIImage imageNamed:@"DaMao"]];
            
            // 登录成功
            UIButton * avatarBTN = [[UIButton alloc]initWithFrame:CGRectMake(20, 20, 60, 60)];
            self.avatarBTN = avatarBTN;
            [self.avatarBTN setBackgroundImage:[UIImage imageNamed:@"DaMao.jpg"] forState:UIControlStateNormal];
            self.avatarBTN.layer.cornerRadius = 30.0f;
            self.avatarBTN.layer.masksToBounds = YES;
            [self.avatarBTN addTarget:self action:@selector(clickHeadView) forControlEvents:UIControlEventTouchUpInside];
            [self.btn addSubview:self.avatarBTN];
            
            UILabel *  INC = [[UILabel alloc]initWithFrame:CGRectMake(100, 30, GetScreenWidth*0.6, 21)];
            self.INC = INC;
            INC.text = @"大猫网络科技(北京)股份有限公司";
            INC.font = [UIFont systemFontOfSize:14];
            [self.btn addSubview:INC];
            
            UILabel * vipName = [[UILabel alloc]initWithFrame:CGRectMake(100, 55, GetScreenWidth*0.2, 21)];
            self.vipName = vipName;
            vipName.text = @"daMao.cn";
            vipName.font = [UIFont systemFontOfSize:14];
            [self.btn addSubview:vipName];
            
            UILabel * identification = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(vipName.frame)+10, 55, GetScreenWidth*0.15, 21)];
            self.identification = identification;
            identification.text = @"未认证";
            identification.font = [UIFont systemFontOfSize:14];
            [self.btn addSubview:identification];
            
        }else{
            [self.btn setTitle:@"未登录" forState:UIControlStateNormal];
            [self.btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
        
        [headerView addSubview:self.btn];
        
    }
    return headerView;
}
#pragma mark - head 点击事件
- (void)clickHeadView
{
    if ([self checkIsLogin]) {
        
    }
    LoginViewController * login = [[LoginViewController alloc]init];
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:login];
    [self presentViewController:nav animated:YES completion:^{
        DMLog(@"模态成功了");
    }];
    
    DMLog(@"点击了 headview");
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * cellStr = @"mine";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
        switch (indexPath.section) {
            case 0:{
                NSArray * tempArray = self.dataArray[indexPath.section];
                cell.textLabel.text = tempArray[indexPath.row];
                break;
            }
            case 1:{
                switch (indexPath.row) {
                    case 0:{
                        NSArray * tempArray = self.dataArray[indexPath.section];
                        cell.textLabel.text = tempArray[indexPath.row];
                        
                        break;
                    }
                    case 1:{
                        NSArray * tempArray = self.dataArray[indexPath.section];
                        cell.textLabel.text = tempArray[indexPath.row];
                        break;
                    }
                    case 2:{
                        NSArray * tempArray = self.dataArray[indexPath.section];
                        cell.textLabel.text = tempArray[indexPath.row];
                        break;
                    }
                    default:
                        break;
                }
                break;
            }
            case 2:{
                switch (indexPath.row) {
                    case 0:{
                        NSArray * tempArray = self.dataArray[indexPath.section];
                        cell.textLabel.text = tempArray[indexPath.row];
                        break;
                    }
                    case 1:{
                        NSArray * tempArray = self.dataArray[indexPath.section];
                        cell.textLabel.text = tempArray[indexPath.row];
                        break;
                    }
                    case 2:{
                        NSArray * tempArray = self.dataArray[indexPath.section];
                        cell.textLabel.text = tempArray[indexPath.row];
                        break;
                    }
                    case 3:{
                        NSArray * tempArray = self.dataArray[indexPath.section];
                        cell.textLabel.text = tempArray[indexPath.row];
                        break;
                    }
                    default:
                        break;
                }
                break;
            }
            default:
                break;
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
    
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0:{
            CollectManageViewController *collectVc = [[CollectManageViewController alloc] init];
            [self.navigationController pushViewController:collectVc animated:YES];
        }
            break;
            
        default:
            break;
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
